package com.wang.huni.BlueToothTools;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.util.Log;

import com.wang.huni.Fragments.healthFragment;
import com.wang.huni.util.MainActivity;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;

/**
 * 蓝牙适配器
 */
public class BlueToothController {

    private BluetoothAdapter mAdapter;
    private String mBtName;
    private String dvName;


    public BlueToothController(){
        mAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    public String getBlueToothName(){
        // 获取已绑定的设备列表
        Set<BluetoothDevice> deviceList = mAdapter.getBondedDevices();
        for (BluetoothDevice device : deviceList) {
            Log.d("wang", "name=" + device.getName() + " Mac:" + device.getAddress());
            dvName = device.getName();
        }
        return dvName;
    }


    /**
     * 是否支持蓝牙
     * @return true 支持, false 不支持
     */
    public boolean isSupportBlueTooth(){
        if (mAdapter != null){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 判断当前蓝牙状态
     * @return true 打开, false 关闭
     */
    public boolean getBlueToothStatus(){
        assert (mAdapter != null);
        return mAdapter.isEnabled();
    }


    /**
     * 打开蓝牙
     * @param activity
     * @param requestCode
     */
    public void turnOnBlueTooth(MainActivity activity, int requestCode){
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        activity.startActivityForResult(intent, requestCode);
//        mAdapter.enable();
    }

    /**
     * 关闭蓝牙
     */
    public void turnOffBlueTooth(){
        mAdapter.disable();
    }
}
